import pandas as pd
import numpy as np
import plotly.express as px
import plotly
import datetime as dt
#import altair as alt
from pathlib import Path
import os
# # Filter all warnings.
# # spurious warnings.
# import warnings
# warnings.filterwarnings('ignore')
from IPython.display import HTML
# Toggle butten to hide the code from the notebook
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
# Read in pre-formatted dataset
project_dir = Path().resolve().parents[0]
file_name = os.path.join(project_dir, 'data', 'final', 'all_data.csv')
all_trips = pd.read_csv(file_name)
all_trips.datetime = pd.to_datetime(all_trips.datetime)
all_trips.start_date = pd.to_datetime(all_trips.start_date)
# Mapbox Token
px.set_mapbox_access_token(open(os.path.join(project_dir,'data','raw','mapbox.token')).read())
General questions that I wanted to ask with the data:
# Sample Data
all_trips.head()
fig1 = px.bar(all_trips.groupby('vehicle_type').count().reset_index(), x="vehicle_type", y="start_date",
color = "vehicle_type")
fig1.show()
plotly.offline.plot(fig1, filename='file.html')
fig2 = px.line(all_trips.groupby(['start_date','vehicle_type']).count().reset_index(),
x="start_date", y='a_dist', color='vehicle_type',
labels={'a_dist':'Number of Rentals/Day', 'start_date': 'Date'})
fig2.show()
The reset of the analysis will just be on scooters.
scooter = all_trips[all_trips['vehicle_type'] == 'scooter']
fig2 = px.line(scooter.groupby(['datetime']).count().reset_index(),
x="datetime", y='a_dist', labels={'a_dist':'Rentals/hr', 'Datetime':'Time'},
range_x=[dt.date(2019,8,18), dt.date(2019,9,1)]
)
fig2.show()
scooter2 = scooter.groupby(['datetime']).count().reset_index()
import plotly.graph_objects as go
fig1 = go.Figure()
fig1.add_trace(go.Scatter(x=scooter2['datetime'],
y=scooter2['a_dist'].values.tolist(),
mode = 'lines',
opacity = 1,
# line = dict(color = '#17BECF'),
name = 'Scooter Rentals'))
# Set title
fig1.update_layout(
title_text="Number of Scooter Rentals per Hour",
xaxis = dict(title = 'Date'),
yaxis = dict(title = 'Rentals/hr'))
# Add range slider
fig1.update_layout(
xaxis=go.layout.XAxis(
rangeselector=dict(
buttons=list([
dict(count=1,
label="1d",
step="day",
stepmode="backward"),
dict(count=2,
label="2d",
step="day",
stepmode="backward"),
dict(count=7,
label="7d",
step="day",
stepmode="backward"),
dict(count=14,
label="14d",
step="day",
stepmode="backward"),
dict(count=1,
label="1m",
step="month",
stepmode="backward"),
dict(count=2,
label="2m",
step="month",
stepmode="backward"),
dict(step="all")
])
),
rangeslider=dict(
visible=True
),
type="date"
)
)
fig1.show()
px.scatter(all_trips, x='trip_distance', y='travel_efficiency')